package edu.neusoft.android.yuejian14.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.javabean.simpleResult;
import edu.neusoft.android.yuejian14.model.UserModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {
    private EditText reg_username;
    private EditText reg_password;
    private EditText reg_password2;
    private Button reg_ok;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();       //调用方法
        initEvents();
    }
    public void initViews(){

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
        reg_username= (EditText) findViewById(R.id.reg_username);
        reg_password = (EditText) findViewById(R.id.reg_password);
        reg_password2 = (EditText) findViewById(R.id.reg_passwordaffirm);
        reg_ok= (Button) findViewById(R.id.reg_ok);

        toolbar.setTitle("注册");
        initToolbarNav(toolbar);

    }
    public void initEvents(){
        reg_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("TAG", "onClick register button in RegisterActivity:");
                UserModel userModel= new UserModel();
                Call call = userModel.register(
                       reg_username.getText().toString(),
                        reg_password.getText().toString(),
                        reg_password2.getText().toString());

                Callback<simpleResult> callback =new Callback<simpleResult>() {
                    @Override
                    public void onResponse(Call<simpleResult> call, Response<simpleResult> response) {
                        simpleResult successbean=response.body();
                        if(!"0".equals(successbean.getSuccess())){
                            Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();

                            String str_username=reg_username.getText().toString();
                            Intent intent=new Intent();
                            intent.putExtra("username",str_username);
                            setResult(RESULT_OK,intent);
                            finish();
                        }
                    }

                    @Override
                    public void onFailure(Call<simpleResult> call, Throwable t) {
                        Toast.makeText(RegisterActivity.this,"注册失败！",Toast.LENGTH_SHORT).show();
                    }
                };
                call.enqueue(callback);

            }
        });

    }


    protected void initToolbarNav(Toolbar toolbar) {

        toolbar.setTitleTextColor(Color.WHITE);
        setSupportActionBar(toolbar);
        //设置显示返回按钮
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //设置返回按钮点击监听
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }
}
